/*
 *   Copyright (C) 2012  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.xmlformatter

import de.kieselbach.javafx.builder.UIBuilder
import de.kieselbach.javafx.UIResources
import javafx.application.Application
import javafx.event.EventHandler
import javafx.scene.Scene
import javafx.scene.control.TextArea
import javafx.scene.layout.BorderPane
import javafx.scene.layout.VBox
import javafx.stage.Stage
import javafx.geometry.Pos
import java.util.logging.Logger
import java.util.logging.Level

class XmlFormatter extends Application {

    final static def UI_RESOURCES_PATH = '/de/kieselbach/xmlformatter/resource/uiresources.xml'

    def mainStage
    def uiResources
    def resourceBundle

    static void main(String[] args) {

        Application.launch(XmlFormatter.class, args)
    }

    @Override
    void start(Stage stage) {

        mainStage = stage
        stage.title = 'XmlFormatter'

        uiResources = new UIResources(getClass().getResourceAsStream(UI_RESOURCES_PATH))
        resourceBundle = uiResources.resourceBundles['gui']
        def uiset = uiResources.createUISet('main')

        // --------------------------
        // set actions

        uiset.actions['exit'] = {e ->
            exit()
        }

        uiset.actions['about'] = {e ->
            about()
        }

        uiset.actions['format'] = {e ->

        }

        uiset.resources['saveas'].disable()

        // --------------------------

        stage.onCloseRequest = [handle: { event ->
            event.consume()
            exit()
        }] as EventHandler

        def vbox = new VBox()
        vbox.children.addAll(uiset.menuBar, uiset.toolBar)

        // -----------------------------

        def textarea = new TextArea()
        textarea.id = 'code-area'

        // -----------------------------

        def borderpane = new BorderPane()
        borderpane.top = vbox
        borderpane.center = textarea
        //borderpane.setBottom(statusbar);

        def scene = new Scene(borderpane, 1024, 768)
        scene.stylesheets.add('de/kieselbach/xmlformatter/stylesheet.css');

        stage.scene = scene
        stage.show()
    }

    void exit() {

        mainStage.close()
    }

    void about() {

        def abouttext = ""
        def reader = null
        try {
            reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(resourceBundle.getString('about.text.uri')), 'ISO-8859-15'))
            def str = reader.readLine()
            while (str != null) {
                abouttext += str
                str = reader.readLine()
                if (str != null) {
                    abouttext += '\n'
                }
            }
        }
        catch (Exception ex) {
            Logger.getLogger(getClass().name).log(Level.WARNING, 'Unable to load the about text!', ex)
        }
        finally {
            if (reader != null) {
                reader.close()
            }
        }

        def dlg
        def closedlg = {e ->
            dlg.close()
        }

        def builder = new UIBuilder()
        dlg = builder.stage('Test', owner: mainStage, modal: true, resizable: false, pack: true, center: true) {
            scene(stylesheets: mainStage.scene.stylesheets) {
                borderPane(padding: [8, 8, 8, 8]) {
                    center(margin: [0, 0, 20, 0]) {
                        text(abouttext, id: 'non-editable-area__')
                    }
                    bottom {
                        bar(spacing: 20, alignment: Pos.CENTER) {
                            button('OK', minWidth: 100, image: uiResources.images['ok16'], action: closedlg)
                            button('Abbrechen', minWidth: 100, image: uiResources.images['cancel16'], action: closedlg)
                        }
                    }
                }
            }
        }

        dlg.show()

        /*
        def panebuilder = new PaneBuilder()
        dialog = panebuilder.dialog(mainframe.resourceBundle.getString('about.title'), owner: mainframe.frame, modal: true, resizable: false, pack: true, center: true) {
            pane() {
                widget(area)
                bar(type: PaneBuilder.DEFAULT)  {
                    button = close(default: true, actionPerformed: {
                        dialog.visible = false
                    })
                }
            }
        }
        */

        /*
        Stage dialog = new Stage()
        dialog.initModality(Modality.WINDOW_MODAL)
        dialog.initOwner(mainStage)
        dialog.setScene(new Scene(VBoxBuilder.create().
                children(new Text("Hi"), new Button("Ok.")).
                alignment(Pos.CENTER).padding(new Insets(5)).build()))
        dialog.show()
        */
    }
}
